
import stores from '@/store';
import { defineNuxtPlugin } from 'nuxt/app';

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.directive('btnPermission',async (el, binding) => {
      if(binding.value == 'all'){
        el.style.display = ''
      }else{
        let melist = await stores.useBaseDataStore().findMenuType3Perms()
        let bool = false
        melist.forEach(item=>{
          if(item== binding.value){
            bool = true
          }
        })
        if(!bool){
          el.style.display = 'none'
        }else{
          el.style.display = ''
        }
      }

  })
  nuxtApp.vueApp.directive('btnPermissionText',async (el, binding) => {
      if(binding.value == 'all' || binding.value == ''|| binding.value == null || binding.value == undefined){
        el.style = el.style + ';pointer-events: block;color:#18B3AB;'
      }else{
        let melist = await stores.useBaseDataStore().findMenuType3Perms()
        let bool = false
        melist.forEach(item=>{
          if(item== binding.value){
            bool = true
          }
        })
        if(!bool){
          el.style = el.style + ';pointer-events: none;color:#333 !important;'
        }else{
          el.style = el.style + ';pointer-events: block;color:#18B3AB;'
        }
      }

  })
  // 添加全局路由守卫
  addRouteMiddleware('manage-redirect', async (to, from) => {
    // 获取用户权限信息
    const baseDataStore = stores.useBaseDataStore()
    if (getToken()) {
      if(!baseDataStore.menuList || !baseDataStore.enums|| !baseDataStore.Storage || baseDataStore.menuList.length < 1 || baseDataStore.enums.length < 1 || baseDataStore.Storage.length < 1 ){
        await baseDataStore.initializeData() 
      }
    }
  

  }, { global: true })
})